﻿using System;
using System.Collections.Generic;
using Moe.ECamel.Common.Contracts;
using Moe.ECamel.Common.Entities;
using Moe.ECamel.Client.ServiceProxies;

namespace Moe.ECamel.Client.Controllers
{
	public class RentalsController : EntityController<Rental>
	{
		private bool _activeOnly;

		public RentalsController(int customerId) 
			: base(customerId)
		{
			_activeOnly = true;
		}

		public bool ActiveOnlyFilter
		{
			get { return _activeOnly; }
			set { _activeOnly = value; }
		}

		public override ECamelError Update(Rental entity)
		{
			var response = ReturnRental(entity, entity.ActualReturnDate.Value);
			return response.Error;
		}

		public ReturnRentalResponse ReturnRental(Rental rental, DateTime dateOfReturn)
		{
			using (var serviceProxy = new RentalServiceProxy())
			{
				return serviceProxy.ReturnItem(rental.RentalId, dateOfReturn);
			}
		}

		public ECamelError PayFine(Rental rental, DateTime paymentDay)
		{
			using (var serviceProxy = new RentalServiceProxy())
			{
				return serviceProxy.PayFine(rental.RentalId, paymentDay);
			}
		}

		protected override IList<Rental> LoadList()
		{
			int customerId = ParentEntityId;
			using (var serviceProxy = new RentalServiceProxy())
			{
				return _activeOnly
				       	? serviceProxy.GetCustomerActiveRentals(customerId)
				       	: serviceProxy.GetCustomerRentals(customerId);
			}
		}
	}
}
